<?php

namespace app\index\controller;
use think\Controller;
use think\View;
use think\Db;
use think\Cache;

class Delete extends Base
{
    public function index()
    {

    }

    public function Sampleindex()
    {
        $res = Model('common')->GetYqdh();
        $this->assign('res', $res);
        return $this->fetch();
    }

    public function Tasksindex()
    {
        $res = Model('common')->GetGcode();
        $this->assign('res', $res);
        return $this->fetch();
    }

    public function Fkindex()
    {
        return $this->fetch();
    }

    public function ybxxindex()
    {
        return $this->fetch();
    }

    /***
     * 根据范围条码 删除
     */

    public function deleteBybetween()
    {
        $startcode = trim(input('post.startcode'));;
        $endcode = trim(input('post.endcode'));
        $data = [
            'code' => $startcode,
            'code' => $endcode
        ];
        $validate = validate('Code');
        if(!$validate->check($data)){
            $this->error($validate->getError(), 'delete/ybxxindex');
        }
        //判断是否存在开始比 后面的大
        if(substr($endcode,-6) < substr($startcode,-6) || substr($startcode,0,6) != substr($endcode,0,6) || (substr($endcode,-6) - substr($startcode,-6)) > 200){
            $this->success('数据不合法 请检查 条码不相同 或者 截止条码比起始条码大 或者 不能相差200个', 'delete/ybxxindex');
        }
        Db::execute("DELETE FROM dbo.Ls_Camera WHERE code  between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        Db::execute("DELETE FROM dbo.Ls_CameraFileRelation WHERE code  between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        Db::execute("DELETE FROM dbo.Ls_MSpecInputFirst WHERE code between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        Db::execute("DELETE FROM dbo.Ls_MSpecInputFirstApplyItem WHERE code between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        Db::execute("DELETE FROM dbo.Ls_MSpecInputSecondApplyItem WHERE code between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        Db::execute("DELETE FROM dbo.f_k_ybxx WHERE ybid   between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        Db::execute("DELETE FROM dbo.f_k_ybxx_picture WHERE ybid  between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        Db::execute("DELETE FROM dbo.t_lis_sampletransPro WHERE ybid  between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        Db::execute("DELETE FROM dbo.t_lis_samplereg WHERE ybid between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        Db::execute("DELETE FROM dbo.S_SampleDetail WHERE s_barcode between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        Db::execute("DELETE FROM dbo.t_lis_tasks WHERE ybid  between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        Db::execute("DELETE FROM t_lis_sampletransPro WHERE ybid between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);
        /*这儿判断是否删除 S_sampleitem*/
        if(input('post.s_code') == 'on'){
            Db::execute("delete from  S_SampleItem where S_SampleId  in (select id from S_SampleDetail where  S_Barcode between :startcode and :endcode)", ['startcode' => $startcode ,'endcode'=>$endcode]);
            Db::execute("delete  from S_SampleDetail  where S_Barcode  between :startcode and :endcode", ['startcode' => $startcode ,'endcode'=>$endcode]);

        }

        //开始记录
        Model('Log')->addLog("删除了一批条码.$startcode.到.$endcode.的信息**", 1, $startcode.'-'.$endcode);
        $this->success('删除成功', 'delete/ybxxindex');
    }

    /***
     * 根据单个条码 删除
     */
    public function deleteByOne()
    {
        $code = trim(input('post.code'));
        $data = [
            'code' => $code
        ];
        $validate = validate('Code');
        if(!$validate->check($data)){
            $this->error($validate->getError(), 'delete/ybxxindex');
        }
        /*开启事务操作*/
        /*       Db::transaction(function() {*/
        Db::execute("DELETE FROM dbo.Ls_Camera WHERE code  = :code", ['code' => $code]);
        Db::execute("DELETE FROM dbo.Ls_CameraFileRelation WHERE code  = :code", ['code' => $code]);
        Db::execute("DELETE FROM dbo.Ls_MSpecInputFirst WHERE code = :code", ['code' => $code]);
        Db::execute("DELETE FROM dbo.Ls_MSpecInputFirstApplyItem WHERE code = :code", ['code' => $code]);
        Db::execute("DELETE FROM dbo.Ls_MSpecInputSecondApplyItem WHERE code = :code", ['code' => $code]);
        Db::execute("DELETE FROM dbo.f_k_ybxx WHERE ybid   = :ybid", ['ybid' => $code]);
        Db::execute("DELETE FROM dbo.f_k_ybxx_picture WHERE ybid  = :ybid", ['ybid' => $code]);
        Db::execute("DELETE FROM dbo.t_lis_sampletransPro WHERE ybid  = :ybid", ['ybid' => $code]);
        Db::execute("DELETE FROM dbo.t_lis_samplereg WHERE ybid = :ybid", ['ybid' => $code]);
        Db::execute("DELETE FROM dbo.S_SampleDetail WHERE s_barcode = :ybid", ['ybid' => $code]);
        Db::execute("DELETE FROM dbo.t_lis_tasks WHERE ybid  = :ybid", ['ybid' => $code]);
        Db::execute("DELETE FROM t_lis_sampletransPro WHERE ybid= :ybid", ['ybid' => $code]);
        /*});*/
        /*这儿判断是否删除 S_sampleitem*/
        if(input('post.s_code') == 'on'){
            Db::execute("delete from  S_SampleItem where S_SampleId in (select id from S_SampleDetail where S_Barcode = :code)", ['code' => $code]);
            Db::execute("delete  from S_SampleDetail  where S_Barcode = :code", ['code' => $code]);
        }
        //开始记录
        Model('Log')->addLog("删除了一个录入的条码.$code.的信息**", 1, $code);
        $this->success('删除成功', 'delete/ybxxindex');
    }

    /***
     * 根据条码 批量删除
     *
     */
    public function deleteByAll()
    {
        $codes = trim(input('post.code'));
        if (empty($codes)) {
            $this->error('不能为空', 'delete/ybxxindex');
        }
        $ArrCode = explode("\n", $codes);
        foreach ($ArrCode as $key => $code) {
            $data = [
                'dh' => $code,
                'mc' => '删除条码',
                'creatime' => date('Y-m-d', time())
            ];
            /*Db::table('xt_hospital')->insert($data);*/
            $redis = new \Redis();
            $redis->connect('192.168.58.110', 6379);
            $redis->auth('ac1995');
            $isPush = $redis->lPush('Delybid', json_encode($data));
        }
        if (!$isPush) {
            $this->error('删除数据加入队列错误', 'delete/ybxxindex');
        }
        //输出发送成功
            //$res = shell_exec("cd C:\phpstudy_pro\WWW\Tools\ &&  php think Delybid");

            Model('Log')->addLog("增加了一批 队列待删除条码.$codes.的信息**", $codes);
            $this->success('删除数据已加入队列 稍等即可删除', 'delete/ybxxindex');
        }



         /*    Db::execute("delete from  S_SampleItem where S_SampleId in (select id from S_SampleDetail where S_Barcode = :code)", ['code' => $code]);
             Db::execute("delete  from S_SampleDetail  where S_Barcode = :code", ['code' => $code]);*/

        /*这儿判断是否删除 S_sampleitem*/
        /*   if (input('post.s_code') == 'on') {
               Db::execute("delete from  S_SampleItem where S_SampleId in (select id from S_SampleDetail where S_Barcode = :code)", ['code' => $code]);
               Db::execute("delete  from S_SampleDetail  where S_Barcode = :code", ['code' => $code]);
               DB::execute("delete from lis_ybxx where ybid = :code and ybzt not in ('p','s')", ['code' => $code]);
           }*/


    /***
     * @param $yqdh
     * @param $S_SampleTime
     * 交接列表数据删除  未接入平台
     */
    public function SampleDelete()
    {
        $yqdh = $_POST['select'];
        $S_SampleTime = $_POST['$S_SampleTime'];
        if (empty($yqdh) || empty($S_SampleTime)) {
            $this->error('不能为空', 'delete/Sampleindex');
        }
        //开始备份
       /* Db::execute("drop table S_SampleDetail_bak_delete_ac");
        $resRes = Db::execute("select * into S_SampleDetail_bak_delete_ac from S_SampleDetail where id in( select a.id  from S_SampleDetail   a 
    left join S_SampleItem b on a.id = b.S_SampleId  where a.yqdh = :yqdh and b.s_online = '0' and S_SampleTime < :S_SampleTime)", ['yqdh' => $yqdh, 'S_SampleTime' => $S_SampleTime]);*/

        //开始删除
        $res = Db::execute("delete from S_SampleDetail where id in( select a.id  from S_SampleDetail   a 
    left join S_SampleItem b on a.id = b.S_SampleId  where a.yqdh = :yqdh and b.s_online = '0' and S_SampleTime < :S_SampleTime)", ['yqdh' => $yqdh, 'S_SampleTime' => $S_SampleTime]);
        if (!$res) {
            $this->error('删除失败', 'delete/Sampleindex');
        }
        //开始记录日志
        Model('Log')->addLog("删除了一批交接列表数据.专业组:$yqdh.*时间：$S_SampleTime*", 1, $S_SampleTime);
        $this->success('删除成功', 'delete/Sampleindex');
    }

    /***
     *      待交接界面删除  未登记的 根据专业组 测试
     */
    public function TasksDelete()
    {
        $gcode = $_POST['select'];
        $djsj = $_POST['djsj'];
        if (empty($gcode) || empty($djsj)) {
            $this->error('不能为空', 'delete/Tasksindex');
        }
        //开始删除
        //任务 接受  就是 zt = 2
        //delete from t_lis_tasks  where zt = '2' and gcode = '3'
        //and  djsj between '2021-06-01' and '2021-07-07'   删除  任务接受  的
        //开始备份
   /*     Db::execute("drop table t_lis_tasks_bak_delete_ac");
        $resCount = Db::execute("select * into t_lis_tasks_bak_delete_ac from  dbo.t_lis_tasks where zt = '0' and gcode = :gcode  and djsj < :djsj", ['gcode' => $gcode, 'djsj' => $djsj]);*/


        $res = Db::execute("delete from  dbo.t_lis_tasks where zt = '0' and gcode = :gcode  and djsj < :djsj", ['gcode' => $gcode, 'djsj' => $djsj]);
        if (!$res) {
            $this->error('待交接删除失败', 'delete/Tasksindex');
        }
        //开始记录日志
        Model('Log')->addLog("删除了一批待交接未登记记录.专业组：$gcode.***登记日期:$djsj.**", 1, $djsj);

        $this->success('删除成功', 'delete/Tasksindex');
    }

    /**
     *      待交接界面删除  未登记的 根据专业组 测试
     */
    public function FkybxxDelete()
    {
        $startybid = $_POST['startybid'];
        $endybid = $_POST['endybid'];
        if (empty($startybid) || empty($endybid)) {
            $this->error('不能为空', 'delete/FkybxxDelete');
        }
        //开始备份
        Db::execute("drop table t_lis_tasks_bak_delete_ac");
        $res = Db::execute("select * into t_lis_tasks_bak_delete_ac from   dbo.t_lis_tasks where zt = '0' and gcode = :gcode  and djsj < :djsj", ['gcode' => $gcode, 'djsj' => $djsj]);

        //开始删除
        $res = Db::execute("delete from  dbo.t_lis_tasks where zt = '0' and gcode = :gcode  and djsj < :djsj", ['gcode' => $gcode, 'djsj' => $djsj]);
        if (!$res) {
            $this->error('待交接删除失败', 'delete/Tasksindex');
        }
        //开始记录日志
        Model('Log')->addLog("删除了一批待交接未登记记录.专业组：$gcode.登记日期:$djsj.**", 1, $djsj);

        $this->success('删除成功', 'delete/Tasksindex');
    }

    public function TasksAcceptIndex(){
        $res = Model('common')->GetGcode();
        $this->assign('res', $res);
        return $this->fetch('delete/TasksAcceptIndex');
    }

    /***
     *      待交接界面  任务接受
     */
    public function TasksAcceptDelete()
    {
        $gcode = $_POST['select'];
        $djsjstart = $_POST['djsjstart'];
        $djsjend = $_POST['djsjend'];
        if (empty($gcode) || empty($djsjstart) || empty($djsjend)) {
            $this->error('不能为空', 'delete/TasksAcceptIndex');
        }
        //开始删除
        //任务 接受  就是 zt = 2
        //delete from t_lis_tasks  where zt = '2' and gcode = '3'
        //and  djsj between '2021-06-01' and '2021-07-07'   删除  任务接受  的
        //开始备份
      /*  Db::execute("drop table t_lis_tasks_bak_delete_ac");
        $coutRes = Db::query("select * into t_lis_tasks_bak_delete_ac from  dbo.t_lis_tasks where zt = '2' and gcode = :gcode 
                             and djsj between :djsjstart and :djsjend",
            ['gcode' => $gcode, 'djsjstart' => $djsjstart, 'djsjend' => $djsjend]);
        $coutRes= sizeof($coutRes);
        if($coutRes > 3000){
            $this->error('待交接删除失败 大于3000 谨慎操作', 'delete/TasksAcceptIndex');
        }*/

        //开始删除
        $res = Db::execute("delete from  dbo.t_lis_tasks where zt = '2' and gcode = :gcode 
                             and djsj between :djsjstart and :djsjend",
            ['gcode' => $gcode, 'djsjstart' => $djsjstart, 'djsjend' => $djsjend]);

        if (!$res) {
            $this->error('待交接删除失败', 'delete/TasksAcceptIndex');
        }
        //开始记录日志
        Model('Log')->addLog("删除了一批待交接任务接受记录.专业组：$gcode.***开始登记日期:$djsjstart.**结束登记日:$djsjend.**总计.$res.个", 1);

        $this->success("删除成功.总计.$res.个", 'delete/Tasksindex');
    }


    /***
     * 语句执行
     */
    public function execindex()
    {
        return $this->fetch('delete/execindex');
    }

    public function exec()
    {
        $sql = $_POST['sql'];
        if (empty($sql)) {
            $this->success('删除失败 不能为空', 'delete/execindex');
        }
        //开始删除
        $res = Db::execute($sql);
        if ($res) {
            //开始记录日志
            Model('Log')->addLog("sql操作：$sql..**", 1, $sql);
            $this->success('执行成功', 'delete/execindex');
        }
        $this->success('执行失败 不能为空', 'delete/execindex');
    }
}